package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.domain.BidRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int insert(UserInfo userInfo) {
        return userInfoMapper.insert(userInfo);
    }

    @Override
    public int update(UserInfo userInfo) {
        int count = userInfoMapper.updateByPrimaryKey(userInfo);
        if (count == 0) {
            throw new DisplayableException("系统繁忙,请稍后再试");
        }
        return count;
    }


    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveBasicInfo(UserInfo userInfo) {
        UserInfo current = this.getCurrent();
        current.setEducationBackground(userInfo.getEducationBackground());
        current.setIncomeGrade(userInfo.getIncomeGrade());
        current.setMarriage(userInfo.getMarriage());
        current.setKidCount(userInfo.getKidCount());
        current.setHouseCondition(userInfo.getHouseCondition());
        //给用户填写基本信息的状态码
        if (!current.getHasBasicInfo()) {
            current.addState(BitStateUtil.HAS_BASICINFO);
        }
        this.update(current);

    }

    @Override
    public UserInfo getCurrent() {
        return userInfoMapper.selectByPrimaryKey(UserContext.getCurrent().getId());
    }

    @Override
    public UserInfo select(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }
}
